#include <iostream>
#include <string.h>
#include <vector>
#include <unistd.h>
#include <stdio.h>
#include "sdks.h"
#include "sdks_media.h"
#include "cJSON.h"
#include "jingyang.h"
//#include <mosquittopp.h>
//#include <exception>

static void disconn_cb_func(unsigned int handle, void *p_obj, int type)
{
	printf("*******disconn_cb_func ****** handle[%d] type[%d] \n", handle, type);
}

unsigned int m_handle = 0;

int get_area_temperature(const char *ip, unsigned short port, const char *user, const char *pwd, 
		int channel, unsigned short area_oper_type, char **pp_result)
{
	int ret = 0;

	ret = sdks_dev_init("");
	if (ret != 0)
	{
		return -1;
	}
	printf("init end! \n");
	printf("ip = %s\n", ip);
	m_handle = sdks_dev_conn(ip, port, user, pwd, disconn_cb_func, NULL);
	if (m_handle <= 0)
	{
		printf("sdk_con error %d \n", m_handle);
		return -1;
	}
	printf("start_sdk! \n");

	//ptz
	//ret = sdks_dev_open_ptz(m_handle);
	//ret = sdks_dev_ptz_preset(m_handle, 1, 1, 15);
	//printf("ptz ret = %d\n",ret);
	//（2）接口调用
	char *p_result = NULL;
	char *p_set_json = NULL;
	char *p_param = NULL;
	static int num = 43;
	long code = 0;
	switch (num)
	{
	case 43:
	{
		ret = sdks_dev_open_ptz(m_handle);
		if (ret != 0)
		{
			return -1;
		}
		printf("return ret ptz_start:%d \n", ret);
		ret = sdks_dev_get_ptz_postion(m_handle, channel, &p_result);
		printf("return ret ptz:%d \n", ret);
		// 获取特征温度请求
		char *param = NULL;
		cJSON *p_root = cJSON_CreateObject();
		cJSON_AddNumberToObject(p_root, "chn_id", channel);
		cJSON_AddNumberToObject(p_root, "area_oper_type", area_oper_type);
		param = cJSON_PrintUnformatted(p_root);
		int ret = sdks_get_thermal_area_feature_temperature(m_handle, param, pp_result);
		printf("return ret:%d \n", ret);
		printf("return pp_result:%s \n", *pp_result);
		cJSON_Delete(p_root);
		break;
	}

	default:
		break;
	}

	//（3）设备断开及释放内存
	sdks_dev_conn_close(m_handle);  //断开连接
	sdks_dev_quit();  //释放内存

	return 0;
}

int init_sdk()
{
	int ret = 0;

	ret = sdks_dev_init("");
	if (ret != 0)
	{
		return -1;
	}
	printf("init end! \n");
	return ret;
}

int release_sdk()
{
	printf("release_sdk\n");
	sdks_dev_quit();  //释放内存
	printf("done\n");
	return 0;
}

unsigned int open_conn(const char *ip, unsigned short port, const char *user, const char *pwd)
{
	printf("ip = %s\n", ip);
	unsigned int handle = sdks_dev_conn(ip, port, user, pwd, disconn_cb_func, NULL);
	return handle;
}

int is_opening_conn(unsigned int handle)
{
	printf("invoking sdks_dev_get_con_sta\n");
	int status = sdks_dev_get_con_sta(handle);
	printf("status=%d\n", status);
	return status;
}

int close_conn(unsigned int handle)
{
	printf("invoking sdks_dev_conn_close\n");
	sdks_dev_conn_close(handle);  //断开连接
	printf("done\n");
	return 0;
}

int get_area_temperature_by_handle(unsigned int handle, 
		int channel, unsigned short area_oper_type, char **pp_result)
{
	int ret = 0;
	//（2）接口调用
	char *p_result = NULL;
	char *p_set_json = NULL;
	char *p_param = NULL;
	static int num = 43;
	long code = 0;
	switch (num)
	{
	case 43:
	{
		/*
		ret = sdks_dev_open_ptz(handle);
		if (ret != 0)
		{
			return -1;
		}
		printf("return ret ptz_start:%d \n", ret);
		ret = sdks_dev_get_ptz_postion(handle, channel, &p_result);
		printf("return ret ptz:%d \n", ret);
		*/
		// 获取特征温度请求
		char *param = NULL;
		cJSON *p_root = cJSON_CreateObject();
		cJSON_AddNumberToObject(p_root, "chn_id", channel);
		cJSON_AddNumberToObject(p_root, "area_oper_type", area_oper_type);
		param = cJSON_PrintUnformatted(p_root);
		int ret = sdks_get_thermal_area_feature_temperature(handle, param, pp_result);
		printf("return ret:%d \n", ret);
		printf("return pp_result:%s \n", *pp_result);
		cJSON_Delete(p_root);
		break;
	}

	default:
		break;
	}

	return 0;
}

JNIEXPORT jstring JNICALL Java_Jingyang_getAreaTemperature 
    (JNIEnv *env, jobject obj, jstring ip, jint port, jstring user, jstring pwd, 
	jint channel, jint area_oper_type) {
	
	printf("Invoking getAreaTemperature");

	const char *cIp = env->GetStringUTFChars(ip, 0);
	const char *cUser = env->GetStringUTFChars(user, 0);
	const char *cPwd = env->GetStringUTFChars(pwd, 0);

	unsigned short cPort = (unsigned short)port;
	unsigned short cChannel = (unsigned short)channel;
	int cAreaOperType = (int)area_oper_type;
	
	printf("ip=%s, port=%d, user=%s, pwd=%s, channel=%d, area_oper_type=%d", cIp, cPort, cUser, cPwd, cChannel, cAreaOperType);

	// use your string
	char *p_result = NULL;
	int ret = get_area_temperature(cIp, port, cUser, cPwd, cChannel, cAreaOperType, &p_result);

	env->ReleaseStringUTFChars(ip, cIp);
	env->ReleaseStringUTFChars(user, cUser);
	env->ReleaseStringUTFChars(pwd, cPwd);

	return env->NewStringUTF(p_result);
}

JNIEXPORT jint JNICALL Java_Jingyang_initSdk 
    (JNIEnv *env, jobject obj) {
	
	printf("Invoking initSdk");

	int ret = init_sdk();

	return ret;
}

JNIEXPORT jint JNICALL Java_Jingyang_releaseSdk 
    (JNIEnv *env, jobject obj) {
	
	printf("Invoking releaseSdk");

	int ret = release_sdk();

	return ret;
}

JNIEXPORT jint JNICALL Java_Jingyang_openConn 
    (JNIEnv *env, jobject obj, jstring ip, jint port, jstring user, jstring pwd) {
	
	printf("Invoking openConn");

	const char *cIp = env->GetStringUTFChars(ip, 0);
	const char *cUser = env->GetStringUTFChars(user, 0);
	const char *cPwd = env->GetStringUTFChars(pwd, 0);

	unsigned short cPort = (unsigned short)port;
	
	printf("ip=%s, port=%d, user=%s, pwd=%s", cIp, cPort, cUser, cPwd);

	unsigned int handle = open_conn(cIp, port, cUser, cPwd);

	env->ReleaseStringUTFChars(ip, cIp);
	env->ReleaseStringUTFChars(user, cUser);
	env->ReleaseStringUTFChars(pwd, cPwd);

	return (jint)handle;
}

JNIEXPORT jint JNICALL Java_Jingyang_closeConn 
    (JNIEnv *env, jobject obj, jint handle) {
	
	printf("Invoking closeConn");

	unsigned int uHandle = (unsigned int)handle;

	int ret = close_conn(uHandle);

	return ret;
}

JNIEXPORT jint JNICALL Java_Jingyang_isOpeningConn 
    (JNIEnv *env, jobject obj, jint handle) {
	
	printf("Invoking isOpeningConn");

	unsigned int uHandle = (unsigned int)handle;

	int ret = is_opening_conn(uHandle);

	return (jint)ret;
}

JNIEXPORT jstring JNICALL Java_Jingyang_getAreaTemperatureByHandle 
    (JNIEnv *env, jobject obj, jint handle, 
	jint channel, jint area_oper_type) {
	
	printf("Invoking getAreaTemperatureByHandle");

	unsigned int uHandle = (unsigned short)handle;
	unsigned short cChannel = (unsigned short)channel;
	int cAreaOperType = (int)area_oper_type;

	// use your string
	char *p_result = NULL;
	int ret = get_area_temperature_by_handle(uHandle, cChannel, cAreaOperType, &p_result);

	return env->NewStringUTF(p_result);
}
